package com.shangqing.system.service.impl;

import com.shangqing.system.entity.*;
import com.shangqing.system.mapper.SysEarthquakeMapper;
import com.shangqing.system.mapper.SysFloodMapper;
import com.shangqing.system.service.ISysFloodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("ISysFloodService")
public class SysFloodServiceImpl implements ISysFloodService {
	@Autowired
	public SysFloodMapper sysFloodMapper;
	@Autowired
	public SysEarthquakeMapper sysEarthquakeMapper;

	/**
	 * 所有洪涝灾害信息
	 *
	 * @return
	 */
	@Override
	public List<Flood> selectfloodinfo() {

		List<Flood> flood = new ArrayList<>();
		List<Flood> floods = sysFloodMapper.floodinfo();
		for (Flood eee : floods
		) {
			SysCity sysCity = sysEarthquakeMapper.selectcity(eee.getParentsource());
			sysCity.setSysCity(City(sysCity));
			eee.setSysCity(sysCity);
			flood.add(eee);
		}
		return flood;
	}

	/**
	 * 循环
	 *
	 * @return
	 */
	public SysCity City(SysCity sysCity) {
		if (sysCity.getParentId() == 0) {
			return null;
		} else {
			SysCity sysCity1 = sysEarthquakeMapper.selectcity(sysCity.getParentId());
			SysCity sysCity2 = new SysCity();
			sysCity2.setId(sysCity1.getId());
			sysCity2.setCityName(sysCity1.getCityName());
			sysCity2.setSysCity(City(sysCity1));
			return sysCity2;
		}

	}

	/**
	 * 预警信息
	 *
	 * @return
	 */
	@Override
	public List<Floodwarning> selectwarning() {
		List<Floodwarning> floodwarnings = sysFloodMapper.warninginfo();
		return floodwarnings;
	}

	/**
	 * 查询每个城市洪涝所占百分比
	 *
	 * @return
	 */
	@Override
	public List<FloodNumber> selectfloodnum() {

		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(0);
		List<FloodNumber> floodNumbers = sysFloodMapper.selectnum();
		Float a = (float) sysFloodMapper.selectall();
		System.out.println(a);
		List<FloodNumber> list = new ArrayList();
		for (FloodNumber e : floodNumbers) {
			if (a != null && floodNumbers != null) {
				String city = e.getCityName();
				float num = e.getNum();
				String sum = numberFormat.format(num / a * 100);
				FloodNumber floodNumber = new FloodNumber();
				floodNumber.setCityName(city);
				floodNumber.setNum(Float.parseFloat(sum));
				list.add(floodNumber);
			}
		}
		return list;
	}

	/**
	 * 按年统计每个城市不同洪涝级别的次数
	 *
	 * @return
	 */
	@Override
	public List<Floodall> selectcounttype(int year) {
		List<Floodall> floodalls = sysFloodMapper.floodall(year);
		List<Floodall> list = new ArrayList<>();
		for (Floodall city : floodalls
		) {
			String cityname = city.getCityName();
			String type = city.getType();
			int count = city.getCount();
			Floodall floodall = new Floodall();
			floodall.setCityName(cityname);
			floodall.setType(type);
			floodall.setCount(count);
			list.add(floodall);
			System.out.println(cityname + type + count);
		}
		return list;
	}

	/**
	 * 全区水位预警
	 * @return
	 */
	@Override
	public List<Map> selecteveryflood() {
		List<Map> maps = sysFloodMapper.seleveryflood();
		return maps;
	}

	/**
	 *  全区灾情统计（近五个月）
	 * @return
	 */
	@Override
	public List<Map> selectfivemonth() {
		List<Map> maps = sysFloodMapper.selfivemonth();
		return maps;
	}

}
